<?php
namespace Home\Controller;

use Think\Controller;

class ServiceItemController extends BaseController {
// class ServiceItemController extends Controller {
	
    public function index(){
    	$setting = getGlobleConfig();
    	$this->assign('service_hotline', $setting ['service_hotline']);
    	
    	$this->assign('select', 'ServiceItem');
    	$PhotoModel = D('Photo');
    	$carouselPhoto = $PhotoModel->findHomeCarousel();
    	$carouselPhoto = multi_array_sort($carouselPhoto, 'sort', SORT_DESC);
    	$this->assign('carousels', $carouselPhoto);
    	
    	$ServiceItemModel = D('ServiceItem');
    	if (!$this->selectCity) {
	    	$openid = $this->getOpenId();
	    	$wxUser = M('wx_visitor')->where(array('openid' => $openid))->find();
	    	$this->assign('currentCity', $wxUser ['wx_city']);
    	}
    	
    	$openCity = D('BeautyParlor')->getOpenCity();
    	
    	$openCity = array_map(function ($var) {
    		$var ['format_city'] = str_replace('市', '', $var ['city']);
    		return $var;
    	}, $openCity);
    	
        $this->assign('detectLocation', $_SERVER['HTTP_REFERER']?0:1);
    	$this->assign('citys', $openCity);
    	
        $this->display();
    }
    
    /**
     * 服务详情
     */
    public function detail() {
    	$id = I ('get.id');
    	$beauticianId = I ('get.beautician_id');
    	$beautyId = I ('get.beauty_id');
    	$selectTime = I('get.selectTime');
    	
    	if (!$id) {
    		redirect(U('index'));
    	}
    	
    	$Model = D('ServiceItem');
    	$item = $Model->getServiceInfo($id);
    	
    	if ($beauticianId || $beautyId) {
    		$this->assign('scrollToBottom', true);
    	}
    	
    	$BeauticianModel = D('Beautician');
    	$BeautyParlorModel = D('BeautyParlor');
    	
    	if (!$beauticianId && !$beautyId) {
    		$this->assign('isDefault', true);
    	} else {
    		if ($beautyId) {
    			$BeautiParlorInfo = $BeautyParlorModel->getBeautyInfo($beautyId);
    			if (!$beauticianId) {
    				$beauticianInfo = $this->getDefaultBeautician($beautyId, $item ['service_time']);
    			}
    		} else {
    			$BeautiParlorInfo = $BeautyParlorModel->getBestBeautyOfBeautician($beauticianId);
    		}
    		
    		if ($beauticianId) {
    			if ($beautyId) {
    				$existEmp = $BeauticianModel->checkBeauticianIsEmploye($beautyId, $beauticianId);
    				if ($existEmp) {
    					$beauticianInfo = $BeauticianModel->getBeauticianInfo($beauticianId);
    				} else {
    					$beauticianInfo = $BeauticianModel->getDefautlBeuatician($beautyId);
    				}
    			} else {
    				$beauticianInfo = $BeauticianModel->getBeauticianInfo($beauticianId);
    			}
    		}
    		
    		if ($beauticianInfo ['status'] != '1') {
    			$beauticianInfo = $this->getDefaultBeautician($BeautiParlorInfo['id'], $item ['service_time']);
    		}
    	}
    	
    	if ($beauticianInfo) {
    		if ($beauticianInfo ['professional_score'] || $beauticianInfo ['punctuality_score'] || $beauticianInfo ['communication_score']) {
    			$beauticianInfo ['start'] = ceil(($beauticianInfo ['professional_score'] + $beauticianInfo ['punctuality_score'] + $beauticianInfo ['communication_score'])/3/$beauticianInfo ['comment_total']);
    		} else {
    			$beauticianInfo ['start'] = 0;
    		}
    	}
    	
    	if ($BeautiParlorInfo) {
    		if ($BeautiParlorInfo ['professional_score'] || $BeautiParlorInfo ['punctuality_score'] || $BeautiParlorInfo ['communication_score']) {
    			$BeautiParlorInfo ['start'] = ceil(($BeautiParlorInfo ['professional_score'] + $BeautiParlorInfo ['punctuality_score'] + $BeautiParlorInfo ['communication_score'])/3/$BeautiParlorInfo ['comment_total']);
    		} else {
    			$BeautiParlorInfo ['start'] = 0;
    		}
    	}
    	
    	$this->assign('beautician', $beauticianInfo);
    	$this->assign('beauty', $BeautiParlorInfo);
    	
    	$PhotoModel = D('Photo');
    	$Content = D('Content');
    	$steps = $Content->getTargetContent($id);
    	
    	$PhotoModel->type = 'carousel';
    	$covers = $PhotoModel->getTargetPhotos($id);
    	
    	$ServiceUseProduct = D('ServiceUseProduct');
    	$useProducts = $ServiceUseProduct->getTargetUseProduct($id);
    	$products = array();
    	$Product = D('Product');
    	foreach ($useProducts as $key => $value) {
    		$products [$key] = $Product->getProductInfo($value ['product_id']);
    		
    		$effects = $Content->getTargetContent($value ['product_id']);
    		$products [$key] ['effects'] = $effects;
    	}
    	
    	$this->assign('select_time', $selectTime);
    	$this->assign('products', $products);
    	$this->assign('covers', $covers);
    	$this->assign('steps', $steps);
    	$this->assign('hasMoreStep', count($steps) > 3);
    	$this->assign('today', time());
    	$this->assign('item', $item);
    	$this->display();
    }
    
    public function recommend() {
    	$province = I ('get.province');
    	$city = I ('get.city');
    	
    	$lat = I('get.lat');
    	$lng = I('get.lng');
    	
    	$serviceId = I ('get.id');
    	$Model = D('ServiceItem');
    	 
    	$item = $Model->getServiceInfo($serviceId);
    	
    	$serviceTime = $item ['service_time'];
    	$BeautyParlorModel = D('BeautyParlor');
    	if ($lat && $lng) {
    		$BeautiParlorInfo = $BeautyParlorModel->getDefaultByDistance($province, $city, $lat, $lng);
    	} else {
    		$BeautiParlorInfo = $BeautyParlorModel->getDefautlBeuaty($province, $city);
    	}
    	
    	if ($BeautiParlorInfo) {
    		unset($BeautiParlorInfo ['password']);
    		$BeautiParlorInfo = $BeautiParlorInfo;
    		$BeauticianModel = D('Beautician');
    		//根据时间获取美容师
    		$beauticianInfo = $this->getDefaultBeautician($BeautiParlorInfo ['id'], $serviceTime);
//     		$beauticianInfo = $BeauticianModel->getDefautlBeuatician($BeautiParlorInfo ['id'], $serviceTime);
    		if ($beauticianInfo) {
    			unset($beauticianInfo ['password']);
    		}
    	}
    	
    	$this->ajaxReturn(array('status' => 1, 'beauty' => $BeautiParlorInfo, 'beautician' => $beauticianInfo));
    }
    
    private function getDefaultBeautician($beautyId, $serviceTime) {
    	$BeauticianModel = D('Beautician');
    	$list = $BeauticianModel->getBeautyBeautician($beautyId);
    	
    	if ($list) {
    		$ScheduleModel = M('schedule');
    		$OrderModel = D('Order');
    		
    		$todayDate = date ( 'Y-m-d' );
    		$dayStart = strtotime ( $todayDate . ' 00:00:00' );
    		$dayEnd = strtotime ( $todayDate . ' 23:59:59' );
    		$todayStart = strtotime ( $todayDate . ' 08:00:00' );
    		$todayEnd = strtotime ( $todayDate . ' 22:00:00' );
    		
    		$time = time();
    		foreach ($list as &$value) {
    			$setting = $ScheduleModel->where(array('uid' => $value ['id']))
    			->order('CurrentDate DESC')->limit(1)->find();
    			
    			$totalSetting = 0;
    			if ($time < $todayEnd) {
    				if ($setting) {
    					$setting = unserialize($setting ['workTime']);
    					$todaySetting = $setting [$dayStart];
    					foreach ($todaySetting as $timeValue) {
    						if ($timeValue ['end'] > $time) {
    							if ($timeValue ['start'] > $time) {
    								$totalSetting += ($timeValue ['end'] - $timeValue ['start'])/ 3600;
    							} else {
    								$totalSetting += ceil(($timeValue ['end'] - $time)/ 3600);
    							}
    						}
    					}
    				} else {
    					$totalSetting = ceil(($todayEnd + 3600 - $time)/3600);
    				}
    			} else {
    				$totalSetting = 0;
    			}
    			
    			$orderTimes = array();
    			$orders= $OrderModel->getBeauticianOrderTimes($beauticianId, $todayStart);
    			if ($orders) {
    				foreach ($orders as $key => $order) {
    					$orderTimes [$order ['order_time']] = $order ['order_time'] + $order ['service_time'] * 60;
    				}
    				
    				$value [$orderTimes] = $orderTimes;
    			}
    			
    			$value ['totalSetting'] = $totalSetting;
    		}
    	}
    	
    	$list = multi_array_sort($list, 'totalSetting', 'desc');
    	return current($list);
    }
    
    /**
     * 重新下单
     */
    public function reOrder() {
    	$orderId = I ('get.id');
    	
    	$OrderModel = D('Order');
    	$orderInfo = $OrderModel->getOrderInfo($orderId);
    	
    	
    	$id = $orderInfo ['service_id'];
    	$beauticianId = $orderInfo ['beautician_id'];
    	$beautyId = $orderInfo ['beauty_id'];
    	if (!$id) {
    		redirect(U('index'));
    	}
    	 
    	if ($beauticianId) {
    		$beauticianInfo = D('Beautician')->getBeauticianInfo($beauticianId);
    		$this->assign('beautician', $beauticianInfo);
    	}
    	 
    	if ($beautyId) {
    		$BeautiParlorInfo = D('BeautyParlor')->getBeautyInfo($beautyId);
    		$this->assign('beauty', $BeautiParlorInfo);
    	}
    	 
    	$Model = D('ServiceItem');
    	$PhotoModel = D('Photo');
    	 
    	$item = $Model->getServiceInfo($id);
    	$Content = D('Content');
    	$steps = $Content->getTargetContent($id);
    	 
    	$PhotoModel->type = 'cover';
    	$covers = $PhotoModel->getTargetPhotos($id);
    	 
    	$ServiceUseProduct = D('ServiceUseProduct');
    	$useProducts = $ServiceUseProduct->getTargetUseProduct($id);
    	$products = array();
    	$Product = D('Product');
    	
    	foreach ($useProducts as $key => $value) {
    		$products [$key] = $Product->getProductInfo($value ['product_id']);
    	
    		$effects = $Content->getTargetContent($value ['product_id']);
    		$products [$key] ['effects'] = $effects;
    	}
    	
    	$this->assign('products', $products);
    	$this->assign('covers', $covers);
    	$this->assign('steps', $steps);
    	$this->assign('hasMoreStep', count($steps) > 3);
    	$this->assign('scrollToBottom', true);
    	$this->assign('item', $item);
    	$this->display('detail');
    }
}